package com.ljxt.dt.web.servlet.storemanagementservlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ljxt.dt.service.storemanagementservice.DetailService;
import com.ljxt.dt.service.storemanagementservice.impl.DetailServiceImpl;
import com.ljxt.dt.util.ServletUtil;
import com.ljxt.dt.vo.PageVO;
import com.ljxt.dt.vo.storemanagement.HandlerInfoVO;
import com.ljxt.dt.vo.storemanagement.ReceptDetailVO;
import com.ljxt.dt.vo.storemanagement.ReceptVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 唐春劲
 * @version 1.0.0
 * @date 2020-05-18 17:24
 * @copyright 老九学堂
 */
@WebServlet("/requestPut")
public class RequestPut extends HttpServlet {
    private DetailService admin = new DetailServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id =  req.getParameter("receptNo");

        ReceptVO receptInfo =  admin.selectDetail(id);

        List<ReceptDetailVO> receptDetailVO = admin.selectDetailGoods(id);
        //创建一个pageVO
        PageVO pageVO = new PageVO(receptDetailVO,-1,-1,-1);
        //将它转为js对象
        ObjectMapper objectMapper = new ObjectMapper();


        req.setAttribute("pageVO",objectMapper.writeValueAsString(pageVO));

        req.setAttribute("vo",receptInfo);


        //获取经办信息
        List<HandlerInfoVO> handlerInfoVOS = admin.selectInfo(id);

        //创建一个pageVO
        PageVO<HandlerInfoVO> vo = new PageVO<>(handlerInfoVOS,-1,10,-1);

        req.setAttribute("info",objectMapper.writeValueAsString(vo));

        ServletUtil.forward(req,resp,"requestPut","storeManagement");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取data的数据
        String data = req.getParameter("data");
        //解析成对象
        Gson gson = new Gson();
        Type type= new TypeToken<ArrayList<ReceptDetailVO>>( ) { }.getType();
        ArrayList< ReceptDetailVO > receptDetailVOS =gson.fromJson(data,type);
       //添加商品以及更新操作
        int row = admin.addGoods(receptDetailVOS);
        ObjectMapper objectMapper = new ObjectMapper();
        resp.getWriter().write(objectMapper.writeValueAsString(row));
    }
}
